
const crt = 5;
const { ccclass, property } = cc._decorator;

@ccclass
export default class editCursor extends cc.Component {

    @property(cc.String)
    colorHex: string = '';

    @property(cc.Label)
    label: cc.Label = null;

    // LIFE-CYCLE CALLBACKS:
    private bPos: cc.Vec2 = null;
    private ePos: cc.Vec2 = null;
    private fontSize: number = null;
    private lineHeight: number = null;

    onLoad() {
        let editBoxNode = this.node.parent;
        this.bPos = cc.v2(-editBoxNode.width * editBoxNode.anchorX + crt);
        this.ePos = cc.v2(editBoxNode.width * (1 - editBoxNode.anchorX) - crt);

        let editBox = editBoxNode.getComponent(cc.EditBox);
        if (!editBox) { this.node.active = false; return; }
        this.fontSize = editBox.textLabel.fontSize;
        this.lineHeight = editBox.textLabel.lineHeight;

        let beginHandler = new cc.Component.EventHandler();
        beginHandler.component = 'editCursor';
        beginHandler.target = this.node;
        beginHandler.handler = 'editBegin';
        editBox.editingDidBegan.push(beginHandler);
        let changedHandler = new cc.Component.EventHandler();
        changedHandler.component = 'editCursor';
        changedHandler.target = this.node;
        changedHandler.handler = 'textChanged';
        editBox.textChanged.push(changedHandler);
        let endedHandler = new cc.Component.EventHandler();
        endedHandler.component = 'editCursor';
        endedHandler.target = this.node;
        endedHandler.handler = 'editEnded';
        editBox.editingDidEnded.push(endedHandler);
        let returnHandler = new cc.Component.EventHandler();
        returnHandler.component = 'editCursor';
        returnHandler.target = this.node;
        returnHandler.handler = 'editReturn';
        editBox.editingReturn.push(returnHandler);
    }

    onEnable() {
        this.node.setPosition(this.bPos);
    }
    onDisable() {
        this.node.stopAllActions();
        this.node.opacity = 0;
    }

    start() {
    }

    // update (dt) {}

    private focusAction() {
        let fi = cc.fadeIn(0);
        let fo = cc.fadeOut(0);
        let dl = cc.delayTime(0.5);
        let rpt = cc.repeatForever(cc.sequence(fi, dl, fo, dl));
        return rpt;
    }
    private runFocusAction() {
        this.node.stopAllActions();
        this.node.opacity = 255;
        let act = this.focusAction();
        this.node.runAction(act);
    }
    editBegin() {
        this.textChanged();
        this.runFocusAction();
    }
    textChanged() {
        let editBoxNode = this.node.parent;
        let editBox = editBoxNode.getComponent(cc.EditBox);
        let str = editBox.string;
        this.label.string = str;
        this.label._forceUpdateRenderData(true);
        let width = this.label.node.width;
        let lineWidth = this.ePos.sub(this.bPos).x;
        if (width > lineWidth) {
            if (editBox.inputMode == cc.EditBox.InputMode.ANY) {
                let lineNum = Math.floor(width / lineWidth);
                width = width % lineWidth;
                this.node.setPosition(cc.v2(width - editBoxNode.width * editBoxNode.anchorX + crt,
                    this.lineHeight * lineNum - this.node.height * (1 - this.node.anchorY)));
            }
            else
                width = lineWidth;
        }
        this.node.setPosition(cc.v2(width - editBoxNode.width * editBoxNode.anchorX + crt));
        this.runFocusAction();
    }
    editEnded() {
        this.node.stopAllActions();
        this.node.opacity = 0;
    }
    editReturn() {
        this.node.stopAllActions();
        this.node.opacity = 0;
    }
}
